package com.example.food.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.food.activity.StoreDetailActivity;
import com.example.food.bean.TakeoutSearchBen;
import com.example.home.R;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class TakeoutSearchAdapter extends RecyclerView.Adapter<TakeoutSearchAdapter.MyHoldy> {
    private Context context;
    private List<TakeoutSearchBen.RowsBean> list;

    public TakeoutSearchAdapter(Context context, List<TakeoutSearchBen.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public TakeoutSearchAdapter.MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sear,null);
        return new TakeoutSearchAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TakeoutSearchAdapter.MyHoldy holder, int position) {
        if (list.get(position)!=null){
            Glide.with(context).load(ip+list.get(position).getImgUrl()).into(holder.imgUrl);
            holder.name.setText(list.get(position).getName());
            holder.score.setText("评分: "+String.valueOf(list.get(position).getScore()));
            holder.avgCost.setText("人均: "+String.valueOf(list.get(position).getAvgCost()));
            holder.deliveryTime.setText("配送时间: "+String.valueOf(list.get(position).getDeliveryTime()+"分钟"));
            holder.distance.setText("到店距离: "+String.valueOf(list.get(position).getDistance()+"m"));
            holder.rcv_list.setAdapter(new RestaurantdishesApter(context,list.get(position).getProductList()));
            holder.rcv_list.setLayoutManager(new GridLayoutManager(context,list.get(position).getProductList().size()));
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StoreDetailActivity.class);
                    intent.putExtra("recomId",String.valueOf(list.get(position).getProductList().get(position).getSellerId()));
                    context.startActivity(intent);
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private ImageView imgUrl;
        private TextView name;
        private TextView score;
        private TextView distance;
        private TextView deliveryTime;
        private TextView avgCost;
        private RecyclerView rcv_list;
        private RelativeLayout rootView;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            imgUrl = itemView.findViewById(R.id.imgUrl);
            name = itemView.findViewById(R.id.name);
            score = itemView.findViewById(R.id.score);
            distance = itemView.findViewById(R.id.distance);
            deliveryTime = itemView.findViewById(R.id.deliveryTime);
            avgCost = itemView.findViewById(R.id.avgCost);
            rcv_list = itemView.findViewById(R.id.rcv_list);
            rootView = itemView.findViewById(R.id.rootView);
        }
    }
}
